<?php	

	include("inc/joomlaInt.php");
	include("inc/inc_configuration.php");

if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{	
	
	$qu=$_SESSION["tquery"];
	$t=$_POST["type"];
	$season=$_POST["season"];
	$uneu = $_POST["uneu"];
	
	$datetime = time();
	
	

	if ($t==1)
	{
		
		for($i=0; $i < count($qu); $i++)
		{
			$ex=mysql_query($qu[$i]);
			// echo "Q$i: $qu[$i] <br><br>";
		}
		
		$tid=$_POST["teamab"];
		$tneu=$_POST["teamneu"];
		
		$cneu=$_POST["cneu"];
		$calt=$_POST["calt"];
		
		$q = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tid";
		$qe = mysql_query($q);
		$q = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tneu";
		$qe = mysql_query($q);
			
		$q = "	INSERT INTO $tbl_combinations (userId,teamId,joinDate,active) VALUES ($uneu,$tid,$datetime,1)";
		
		// echo "$q <br><br>";
		$qe = mysql_query($q);
		$q = "	INSERT INTO $tbl_combinations (userId,teamId,joinDate,active) VALUES ($jUser,$tneu,$datetime,1)";
		$qe = mysql_query($q);
		// echo "$q <br><br>";
		
		$q = "SELECT id FROM $tbl_combinations WHERE userId=$jUser AND teamId=$tneu AND active=1";
		$qe = mysql_query($q);
		$qeo = mysql_fetch_object($qe);
		$cId = $qeo->id;
		
		$q = "	INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($cId,1,$datetime,'Teamtausch',$season)";
		$qe = mysql_query($q);	
		// echo "$q <br><br>";

		$q = "SELECT id FROM $tbl_combinations WHERE userId=$uneu AND teamId=$tid AND active=1";
		$qe = mysql_query($q);
		$qeo = mysql_fetch_object($qe);
		$cnId = $qeo->id;
		
		$q = "	INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($cnId,1,$datetime,'Teamtausch',$season)";
		$qe = mysql_query($q);
		// echo "$q <br><br>";		
		
		$q = "DELETE FROM $tbl_ttb WHERE combinationId=$cneu OR combinationId=$calt";
		$qe = mysql_query($q);
		
		$q = "DELETE FROM $tbl_ttb_a WHERE combinationId=$cneu OR combinationId=$calt";
		$qe = mysql_query($q);

		
						
	}
	else
	{
		if ($t==2)
		{
			
			$t1id=$_POST["team"];
			$t2id=$_POST["team2"];
			$tnid=$_POST["teamneu"];
			$tt=$_POST["teamtausch"];
			$uneu=$_POST["uneu"];
			$cneu=$_POST["cneu"];
			$calt=$_POST["calt"];
			$calt2=$_POST["calt2"];
			
			
			$queries = $qu;
			
			
			for($i=0; $i < count($queries); $i++)
			{
				// echo $queries[$i]."<br>";
				$ex=mysql_query($queries[$i]);
				// echo "Q$i: $qu[$i] <br><br>";
			}
			
			$q = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tid";
			$qe = mysql_query($q);
			$q = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tneu";
			$qe = mysql_query($q);
		
			$q = "	INSERT INTO $tbl_combinations (userId,teamId,joinDate,active) VALUES ($jUser,$tt,$datetime,1)";
			$qe = mysql_query($q);
			// echo "$q <br><br>";
			
			$q = "	INSERT INTO $tbl_combinations (userId,teamId,joinDate,active) VALUES ($uneu,$tnid,$datetime,1)";
			$qe = mysql_query($q);
			// echo "$q <br><br>";
			
			$q = "SELECT id FROM $tbl_combinations WHERE userId=$jUser AND teamId=$tt AND active=1";
			$qe = mysql_query($q);
			$qeo = mysql_fetch_object($qe);
			$cId = $qeo->id;
			
			$q = "	INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($cId,1,$datetime,'Teamtausch',$season)";
			$qe = mysql_query($q);
			// echo "$q <br><br>";
			
			$q = "SELECT id FROM $tbl_combinations WHERE userId=$uneu AND teamId=$tnid AND active=1";
			$qe = mysql_query($q);
			$qeo = mysql_fetch_object($qe);
			$cnId = $qeo->id;
			
			$q = "	INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($cnId,1,$datetime,'Teamtausch',$season)";
			$qe = mysql_query($q);
			// echo "$q <br><br>";
			
			
			
			$q = "DELETE FROM $tbl_ttb WHERE combinationId=$cneu OR combinationId=$calt OR combinationId=$calt2";
			$qe = mysql_query($q);
			// echo "$q <br><br>";
			
			$q = "DELETE FROM $tbl_ttb_a WHERE combinationId=$cneu OR combinationId=$calt OR combinationId=$calt2";
			$qe = mysql_query($q);
			// echo "$q <br><br>";
			
		}
	}
	echo "Erfolgreich!<br>";
	echo 'Sie werden weitergeleitet... <meta http-equiv="refresh" content="0; URL=lima.php">   ';

}
?>